<?php

class Application_Model_Mapper_AenderungBootMapper{
	protected $_dbTable;

	public function setDbTable ($dbTable){	
		if (is_string($dbTable)){
			$dbTable = new $dbTable();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract){
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable (){
		if (null === $this->_dbTable){
			$this->setDbTable('Application_Model_DbTable_aenderungboot');
		}
		return $this->_dbTable;
	}

	public function addBoot (Application_Model_Boot $boot, $originalID){		
		$now = new DateTime();
		/**
		 * Fill data array
		 */
		$generatedid = Helper_Idgenerator::getid();
		$generatedidcheck = $this->fetchID($generatedid);
		if(empty($generatedidcheck)){
			$data = array(
					'id' =>$generatedid,
					'originalID'=>$originalID,
					'created' => date('Y-m-d H:i:s'),
					'lastupdater' => $boot->getLastupdater(),
					'lastupdated' => date('Y-m-d H:i:s'),
					'schiffsart' => $boot->getSchiffsart(),
					'schiffsmodell' => $boot->getSchiffsmodell(),
					'breite' => $boot->getBreite(),
					'laenge' => $boot -> getLaenge(),
					'hoehe' => $boot->getHoehe(),
					'rumpf' => $boot->getRumpf(),
					'passagiere' => $boot->getPassagiere(),
					'knoten' => $boot->getKnoten(),
					'reichweite' => $boot->getReichweite(),
					'kabinenanzahl' => $boot->getKabinenanzahl()
			);
// 				'profilbild' => $boot->getProfilbild());
			return $this->getDbTable()->insert($data);
		}
		else{
			// 			$this->addHafen($object);
			$this->addBoot($boot);
		}
	}
	
	public function deleteBoot ($id){
		$table = $this->getDbTable();
		$where = $table->getAdapter()->quoteInto('id = ?', $id);
		$table->delete($where);;
	}

	public function fetchID ($id){
		$db = Zend_Db_Table::getDefaultAdapter();
		$entry = new Zend_Db_Select($db);
		$entry->from('aenderungboot');
		$entry->where('aenderungboot.id = ?', $id);
		return $entry->query()->fetch();
	}
	
	public function fetchAll(){
 		$db = Zend_Db_Table::getDefaultAdapter();
 		$entry = new Zend_Db_Select($db);
		$entry->from('aenderungboot');
		return $entry->query()->fetchall();
	}
	
}